package com.boxin.ims.modules.bi.recharge.web;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.bi.dao.BIDao;
import com.boxin.ims.modules.bi.entity.Series;
import com.boxin.ims.modules.bi.entity.Tooltip;
import com.boxin.ims.modules.bi.recharge.dao.BIRechargeDao;
import com.boxin.ims.modules.bi.utils.DateProcessingUtils;
import com.boxin.ims.modules.sys.scheduler.BiSceneProcessScheduler;
import com.boxin.ims.modules.wechat.entity.WechatScene;
import com.boxin.ims.modules.wechat.service.WechatSceneService;
import com.boxin.ims.modules.wechat.utils.AssertUtils;
import com.boxin.ims.modules.wechat.utils.XMLUtils;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

import freemarker.template.utility.DateUtil;

/**
 * 用户充值统计
 * @author xiaoqian
 *
*	2014上午9:02:59
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/bi/memberRecharge")
public class MemberRechargeController extends BaseController {
	
	@Autowired
	private BIDao biDao;
	
	@Autowired
	private BIRechargeDao biRechargeDao;
	
	
	@Autowired
	BiSceneProcessScheduler biSceneProcessScheduler;
	
	
	
	@RequestMapping(value ="biRechargeLogList")
	public String biRechargeLogList( HttpServletRequest request, HttpServletResponse response, Model model) {
		String time =request.getParameter("time");
		Calendar cal = Calendar.getInstance();
		
		String year="";
		String month="";
		if(time==null||time==""){
			month=DateProcessingUtils.getmonth();
			year=cal.get(Calendar.YEAR)+"";
		}else{
			year=time.substring(0,time.indexOf("年"));
			month=time.substring(time.indexOf("年")+1, time.indexOf("月"));
			if(Integer.parseInt(month)<10){
				month=0+month;
			}
		}
	
		
		User user=UserUtils.getUser();
		
		String sellerIds=request.getParameter("sellerIds");
		if(AssertUtils.notEmpty(sellerIds)){
			String[] userIds= sellerIds.split(",");
			if (user.isAdmin()){
				if(userIds.length==1){
					user=new User(Long.valueOf(userIds[0]));
					model.addAttribute("user", user);
				}
			}
		}
		
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("user_id", user.getId());
		
		List<Map<String, Object>>  list=biRechargeDao.queryRechargeLogByUserId(params);
		
		params.put("month",year+month);
		params.put("type", 0);
		List<Map<String, Object>> rechargeList=biRechargeDao.queryRechargeLogBymonth(params);
		
		params.put("type", 1);
		List<Map<String, Object>> expenseList=biRechargeDao.queryRechargeLogBymonth(params);
		
		params.put("type", 2);
		List<Map<String, Object>> exceptionList=biRechargeDao.queryRechargeLogBymonth(params);
		
		String[] ints =  DateProcessingUtils.daysMountBymonth(month,year);
		
		
		Integer[] recharge=DateProcessingUtils.getData(ints, rechargeList);
		Integer[] expense=DateProcessingUtils.getData(ints, expenseList);
		Integer[] exception=DateProcessingUtils.getData(ints, exceptionList);
		
		
		
		
		
		Map<String, Object>  map= DateProcessingUtils.processingRechargeLog(list);
		
		model.addAttribute("total", map);

		model.addAttribute("time", time);
		model.addAttribute("year", year);
		model.addAttribute("month", month);

		model.addAttribute("recharge", JSON.toJSONString(recharge));
		model.addAttribute("expense", JSON.toJSONString(expense));
		model.addAttribute("exception", JSON.toJSONString(exception));
		
		return "modules/bi/recharge/biRechargeLogList";
	}
	
}

